<template>
  <div class="container">
    <el-card>
      <!-- 面包屑 -->
      <el-header class="mianbaoxie" v-if="ishow">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签页</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>
      <!-- 搜索 -->
      <div class="search">
        <div class="el-col-18">
          <el-form ref="form" :model="form" class="el-form--inline">
            <el-form-item label="目录名称">
              <el-input v-model="form.tagName"></el-input>
            </el-form-item>
            <el-form-item label="状态" class="input2">
              <el-select
                v-model="label1"
                placeholder="请选择"
                @change="zhuangtai(state), $forceUpdate()"
              >
                <el-option
                  v-for="item in option"
                  :key="item.state"
                  :label="item.value"
                  :value="item.state"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clear(state)">清除</el-button>
              <el-button type="primary" size="small" @click="search(state)"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="el-col-6">
          <el-col style="text-align: right">
            <el-button type="text" @click="goBack" v-if="ishow">
              <i class="el-icon-back" />返回学科
            </el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="addxk"
            >
              新增学科</el-button
            >
          </el-col>
        </div>
      </div>
      <!-- 提示 -->
      <div>
        <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon
          :closable="false"
        >
        </el-alert>
      </div>
      <!-- 表格 -->
      <div>
        <template class="biaoge">
          <el-table
            :data="tableData"
            style="width: 100%; text-align: center"
            :show-header="true"
            :header-cell-style="headClass"
          >
            <el-table-column prop="date" type="index" label="序号">
            </el-table-column>
            <el-table-column prop="subjectName" label="学科名称">
            </el-table-column>
            <el-table-column prop="username" label="创建者"> </el-table-column>
            <el-table-column prop="addDate" label="创建日期">
              <template slot-scope="{ row }">
                {{ row.addDate | parseTimeByString }}
              </template>
            </el-table-column>
            <el-table-column prop="tagName" label="标签名称"> </el-table-column>
            <el-table-column prop="state" label="状态">
              <template slot-scope="scope">
                {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <!-- 文字按钮 -->
              <template slot-scope="{ row }">
                <el-button type="text" @click="changeState(row.id, row.state)">
                  {{ row.state === 0 ? '启用' : '禁用' }}
                </el-button>
                <el-button
                  type="text"
                  @click="xiugai(row.id)"
                  :disabled="row.state === 1 ? false : true"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  @click="shanchu(row.id)"
                  :disabled="row.state === 1 ? false : true"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
      <!-- 分页 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          :background="true"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="10"
          :page-sizes="[10, 20, 30, 40]"
          layout="prev, pager,next,sizes,jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增和修改 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="qvxiao"
    >
      <el-form :rules="rules" :model="form">
        <el-form-item label="所属学科">
          <el-select
            v-model="label"
            @change="searchSelect(label), $forceUpdate()"
          >
            <!-- <el-option selected:disabled>{{value}}</el-option> -->
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="sName">
          <el-input v-model="form.sName" class="input3"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="qvxiao">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, detail, update, add, changeState } from '@/api/hmmm/tags'
// 学科列表接口
import { simple } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      total: 0,
      currentPage: 1,
      subjectID: null,
      only: '',
      label1: '',
      disabled: false,
      name: '',
      zlabel: '',
      id: null,
      state: null,
      zstate: null,
      label: '',
      title: '',
      dialogVisible: false,
      page: '1',
      pagesize: '10',
      tableData: [],
      options: [],
      option: [
        { value: '启用', state: 1 },
        { value: '禁用', state: 0 }
      ],
      form: {
        tagName: '',
        sName: ''
      },
      rules: {
        sName: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.allList()
  },
  methods: {
    // 表头样式
    headClass () {
      return 'background:#fafafa;;border-bottom:10px;border-bottom: 2px solid #e8e8e8;'
    },
    // 分页
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.allList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagesize = val
      this.allList()
    },
    // 所有数据
    async allList () {
      // 从学科跳转过来
      const routerParams = this.$route.query
      const { id } = routerParams
      const { name } = routerParams
      this.name = name
      if (id) {
        this.ishow = true
      } else {
        this.ishow = false
      }
      // console.log(routerParams)
      const res = await list({
        subjectID: id,
        tagName: this.form.tagName,
        page: this.page,
        pagesize: this.pagesize,
        state: this.zstate
      })
      this.tableData = res.data.items
      this.total = res.data.counts
      // console.log(res.data.items)
      // 下拉框数值
      const re = await simple()
      this.options = re.data
      // console.log(this.options)
    },
    // 搜索
    search () {
      this.zstate = this.label1
      this.allList()
    },
    // 取消
    qvxiao () {
      this.dialogVisible = false
      this.form.sName = ''
    },
    // 删除
    async shanchu (id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove({ id: id })
      this.allList()
    },
    // select选中的值
    searchSelect (label) {
      // console.log(label)
      this.subjectID = label
    },
    //
    zhuangtai (state) {
      // console.log(state)
    },
    // 清除
    clear () {
      // console.log(state)
      // console.log(this.label1)
      this.form.tagName = null
      this.label1 = null
    },
    // 修改
    async xiugai (id) {
      this.id = id
      this.only = true
      this.title = '修改目录'
      const res = await detail({ id: id })
      this.form.sName = res.data.tagName
      /*  console.log(res.data.subjectID)
      console.log(re.data) */
      this.subjectID = res.data.subjectID
      const sub = this.options.filter((item) => item.value === this.subjectID)
      console.log(sub[0].label)
      this.label = sub[0].label
      this.dialogVisible = true
      /* console.log(id) */
    },
    // 确定
    async submit () {
      if (this.only) {
        await update({
          id: this.id,
          tagName: this.form.sName,
          subjectID: this.subjectID
        })
      } else {
        console.log(this.id)
        await add({ tagName: this.form.sName, subjectID: this.subjectID })
      }
      this.allList()
      this.qvxiao()
    },
    // 新增标签
    addxk () {
      this.label = '请选择'
      this.only = false
      this.title = '新增目录'
      this.dialogVisible = true
    },
    // 禁用与开启
    async changeState (id, state) {
      if (state === 1) {
        this.disabled = true
      } else {
        this.disabled = false
      }
      // console.log(this.disabled)
      this.tableData.state = this.disabled ? 0 : 1
      // console.log(state)
      await changeState({ id: id, state: this.tableData.state })
      this.allList()
    },
    // 回到上一页
    goBack () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  margin: 10px 0;
  margin-left: 10px;
}
.mianbaoxie {
  padding: 0 !important;
  padding-bottom: 30px !important;
  height: 20px !important;
  margin-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
}
.el-form--inline .el-form-item {
  display: inline-block;
  margin-right: 10px;
  vertical-align: top;
}
.el-form-item__content {
  margin-left: 0;
}
.search {
  display: flex;
}
.el-col-6 {
  float: right;
}
.el-col-8 {
  float: left;
}
.el-alert {
  padding: 8px 16px;
  margin-bottom: 15px;
}
.input2 {
  margin-left: 40px;
}
.biaoge {
  text-align: center;
  font-size: 14px;
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.input3 {
  width: 60%;
}
</style>
